package com.yanxibao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanxibao.bean.Um;
import com.yanxibao.common.Result;
import com.yanxibao.dto.PageQueryDTO;
import com.yanxibao.mapper.UmMapper;
import com.yanxibao.service.UmService;
import com.yanxibao.util.QueryUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 院校专业;(_um)表服务实现类
 *
 * @author kongke
 * @date 2023/11/16
 */
@Service
public class UmServiceImpl extends ServiceImpl<UmMapper, Um> implements UmService {

    @Resource
    private UmMapper umMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param umId 主键
     */
    @Override
    public Result<Um> queryById(Integer umId) {
        Um um = getById(umId);
        return null != um ? Result.success(um) : Result.error("500", "未查询到数据");
    }

    /**
     * 查询全部
     *
     * @param um 筛选条件
     */
    @Override
    public Result<List<Um>> queryAll(Um um) {

        QueryWrapper<Um> wrapper = new QueryWrapper<>();
        Integer cls = um.getUmCollection();
        if (cls != null) {
            wrapper.ge("um_collection", cls);
            um.setUmCollection(null);
        }

//        强制转换忽略检查，（已保证返回值不会出现冲突）
        @SuppressWarnings("unchecked")
        List<Um> list = (List<Um>) QueryUtil.FltQuery("eq",um, null, umMapper, wrapper);

        return Result.success(list);
    }

    /**
     * 分页查询
     *
     * @param um      筛选条件
     * @param current 当前页码
     * @param size    每页大小
     */
    @Override
    public Result<PageQueryDTO<Um>> pageQuery(Um um, Integer current, Integer size) {

        if (current == null || size == null || current < 1 || size < 1) {
            return Result.error("03040", "分页参数不正确");
        }



//      添加非等于条件
        QueryWrapper<Um> wrapper = new QueryWrapper<>();
        Integer cls = um.getUmCollection();
        if (cls != null) {
            wrapper.ge("um_collection", cls);
            um.setUmCollection(null);
        }

        Page<Um> page = Page.of(current, size);

//        强制转换忽略检查，（已保证返回值不会出现冲突）
        @SuppressWarnings("unchecked")
        PageQueryDTO<Um> dto = (PageQueryDTO<Um>) QueryUtil.FltQuery("eq",um, page, umMapper, wrapper);

        return Result.success(dto);

    }

    /**
     * 新增数据
     *
     * @param um 实例对象
     */
    @Override
    public Result<Um> insert(Um um) {

        if (um.getUmId() != null) {
            return Result.error("03001", "请勿添加ID信息");
        }
        boolean save = save(um);
        return save ? Result.success(um) : Result.error("03000", "新增数据失败");
    }

    /**
     * 更新数据
     *
     * @param um 实例对象
     */
    @Override
    public Result<Um> update(Um um) {
        boolean update = updateById(um);
        return update ? Result.success(um) : Result.error("03020", "更新数据失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param umId 主键
     */
    @Override
    public Result<Boolean> deleteById(Integer umId) {
        boolean remove = removeById(umId);
        return remove ? Result.success(true) : Result.error("03030", "删除数据失败");
    }


}